#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QVBoxLayout>
#include <QMessageBox>
#include <iostream>

// 基类：CustomWidget，继承自 QWidget
class CustomWidget : public QWidget {
    Q_OBJECT
public:
    explicit CustomWidget(QWidget *parent = nullptr) : QWidget(parent) {
        std::cout << "CustomWidget constructor" << std::endl;
    }

    virtual ~CustomWidget() {
        std::cout << "CustomWidget destructor" << std::endl;
    }

    // 虚函数示例
    virtual void showInfo() {
        QMessageBox::information(this, "Info", "This is a CustomWidget");
    }
};

// 派生类：CustomButton，继承自 CustomWidget
class CustomButton : public CustomWidget {
    Q_OBJECT
public:
    explicit CustomButton(const QString &text, QWidget *parent = nullptr)
        : CustomWidget(parent) {
        button = new QPushButton(text, this);
        connect(button, &QPushButton::clicked, this, &CustomButton::onButtonClicked);

        QVBoxLayout *layout = new QVBoxLayout(this);
        layout->addWidget(button);

        std::cout << "CustomButton constructor" << std::endl;
    }

    ~CustomButton() {
        std::cout << "CustomButton destructor" << std::endl;
    }

    // 重写虚函数
    void showInfo() override {
        QMessageBox::information(this, "Info", "This is a CustomButton");
    }

private slots:
    void onButtonClicked() {
        std::cout << "CustomButton clicked" << std::endl;
        showInfo();
    }

private:
    QPushButton *button;
};

// 另一个派生类：CustomLabel，继承自 CustomWidget
class CustomLabel : public CustomWidget {
    Q_OBJECT
public:
    explicit CustomLabel(const QString &text, QWidget *parent = nullptr)
        : CustomWidget(parent) {
        label = new QLabel(text, this);
        label->setAlignment(Qt::AlignCenter);

        QVBoxLayout *layout = new QVBoxLayout(this);
        layout->addWidget(label);

        std::cout << "CustomLabel constructor" << std::endl;
    }

    ~CustomLabel() {
        std::cout << "CustomLabel destructor" << std::endl;
    }

    // 重写虚函数
    void showInfo() override {
        QMessageBox::information(this, "Info", "This is a CustomLabel");
    }

    void setText(const QString &text) {
        label->setText(text);
    }

private:
    QLabel *label;
};

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    // 创建主窗口
    QWidget mainWindow;
    mainWindow.setWindowTitle("Qt Inheritance Example");
    mainWindow.resize(400, 300);

    // 创建布局
    QVBoxLayout *mainLayout = new QVBoxLayout(&mainWindow);

    // 创建派生类的实例
    CustomButton *button = new CustomButton("Click Me!");
    CustomLabel *label = new CustomLabel("Hello, Qt Inheritance!");

    // 添加到布局
    mainLayout->addWidget(button);
    mainLayout->addWidget(label);

    // 演示多态性
    CustomWidget *widget1 = button;
    CustomWidget *widget2 = label;

    // 创建一个按钮来演示多态调用
    QPushButton *polyButton = new QPushButton("Demonstrate Polymorphism");
    mainLayout->addWidget(polyButton);

    QObject::connect(polyButton, &QPushButton::clicked, [widget1, widget2]() {
        // 通过基类指针调用派生类的重写方法
        widget1->showInfo();
        widget2->showInfo();
    });

    // 显示窗口
    mainWindow.show();

    return app.exec();
}


